package com.zbec.mdrs.service;

import com.zbec.framework.core.util.PageUtils;
import com.zbec.framework.core.vo.JqgridResponse;
import com.zbec.framework.core.vo.PageInfo;
import com.zbec.framework.security.service.SecurityService;
import com.zbec.mdrs.entity.Logs;
import com.zbec.mdrs.repository.jpa.LogsDao;
import com.zbec.mdrs.util.ResourceUtils;
import com.zbec.mdrs.vo.LogsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by fxl on 2015/11/8.
 */
@Component
@Transactional
public class LogsService {

    @Autowired
    private SecurityService securityService;

    @Autowired
    private LogsDao logsDao;


    public void handleBuzLogData(String userIp,String buzType,String actionType,Boolean optState,String detail){

            String logStatus="";
            if(optState){
                logStatus =ResourceUtils.getLocaleString("logs.operation.successful") ;
            } else {
                logStatus = ResourceUtils.getLocaleString("logs.operation.failure");
            }
            Logs log=new Logs();
            log.setOperationType(ResourceUtils.getLocaleString(actionType));
            log.setOperationStatus(logStatus);
            log.setBusinessType(ResourceUtils.getLocaleString(buzType));
            log.setUserIp(userIp);
            log.setUserName(securityService.getCurrentUser().getLoginName());
            log.setOperationDesc(detail);
            log.setOperationTime(new Date(System.currentTimeMillis()));
            log.setLogType("BUZ");
            logsDao.save(log);
    }

    public JqgridResponse<LogsVo> findAllLogs(LogsVo vo,PageInfo pageInfo){
        Page<LogsVo> logsVoVoPage= logsDao.findAllLogs(vo.getStartDate(),vo.getEndDate(),PageUtils.buildPageRequest(pageInfo));
        return PageUtils.buildResponse(logsVoVoPage, logsVoVoPage.getContent());
    }
}
